import type { LoginResult } from '@/types/auth'
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { storage } from '@/stores/persist';
import { isEmpty } from '@/utils/util';

// 定义 Store
export const useAuthStore = defineStore(
	'auth',
	() => {
		const loginPopup = ref(false);
		// 会员信息
		const profile = ref<LoginResult>();

		const isLogin = computed(() => {
			return !isEmpty(profile.value?.access_token);
		});

		// 保存会员信息，登录时使用
		const setProfile = (val : LoginResult) => {
			profile.value = val
		}

		// 清理会员信息，退出时使用
		const clearProfile = () => {
			profile.value = undefined
		}

		function showLogin() {
			loginPopup.value = true;
		}
		function closeLogin() {
			loginPopup.value = false;
		}

		// 记得 return
		return {
			isLogin,
			loginPopup,
			show: showLogin,
			close: closeLogin,
			profile,
			setProfile,
			clearProfile
		}
	},
	{
		// 网页端配置
		// persist: true,
		// 小程序端配置
		persist: {
			storage
		},
	},
)